<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of terrain
 *
 * @author Charles
 */
class terrain {
    
    var $text = array (
        0 => 'Grassland',
        1 => 'Desert',
        2 => 'River',
        3 => 'Volcanic',
        4 => 'Crater',
        5 => 'Glacier',
        6 => 'Ocean',
        7 => 'Swamp',
        8 => 'Cave',
        9 => 'Gas Giant',
        10 => 'Mountain',
        11 => 'Sun',
        12 => 'Black Hole',
        13 => 'Forest',
        14 => 'Jungle',
        15 => 'Rock',
        16 => 'None'
    );

    /**
     * 
     * Querys database and looks for terrain type field
     * and returns it.
     * 
     * @param mixed $coordinates
     * @param int $planetID
     * @global object $mysqli
     * 
     * @return int
     */
    function getTerrainType($coordinates, $planetID) {
        global $mysqli;
        
        // Setup query
        $query = "SELECT type FROM terrain WHERE planet_id = '$planetID' AND
                coordinate = '$coordinates'";
        $result = $mysqli->query($query) or die(mysqli_error($mysqli));
        $row = $result->fetch_array(MYSQLI_ASSOC);
        
        $terrainType = $row['type'];
        return $terrainType;
    }




    /**
     * 
     * @param int $type
     * @return string Either returns the image or the text type
     */
    function displayTerrainType($type, $image=FALSE) {
        
        if($image != FALSE) {
            return $this->image[$type];
        } else {
            return $this->text[$type];
        }
    }
    
    
    /**
     * 
     * @global object $mysqli
     * @param int $planetID
     * @param int $planetSize
     */
    function drawMap($planetID, $planetSize) {
        global $mysqli;
        // setup for loop to loop through x coordinates, and then y
        for ($y = 0; $y <= $planetSize; $y++) {
            for ($x = 0; $x <= $planetSize; $x++) {
                $coordinates = $x.", ".$y;
                // Set a varaible for current terrain type
                $type = $this->getTerrainType($coordinates, $planetID);
                // If Coordinate doesn't equal none terain type display image
                if($type != 16 && $type != NULL && $type != '') {
                    echo '<div class="'.$this->displayTerrainType($type).'"></div>';
                } elseif($type == 16 || $type == NULL) {
                    echo '<div class="none"></div>';
                }
                
                // if all x's drawn do line break
                if ($x == $planetSize) {
                    echo "<br />";
                }
                
            }
        }
    }
}

?>
